package com.syncnews.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.syncnews.entity.VietBaoEntity;



public class NewsGame extends BaseDAONewsGame {

	
	/**
	 * 
	 * @param category
	 * @param content
	 * @param title
	 * @param seoUrl
	 * @param site
	 * @param db
	 * @throws Exception
	 */
	public void insertPost(int category,String content,String title,String seoUrl,String site,String db) throws Exception {
		try {
			//INSERT INTO wp_posts(post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged ,post_modified ,post_modified_gmt,post_content_filtered ,post_parent,guid,menu_order,post_type,post_mime_type,comment_count) VALUES (3, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), content, title, '', 'publish', 'open', 'open', '', seoUrl, "", "", CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), "", '0', 'http://localhost/taiiwin/?p=1', '0', 'post', '', '0');
			final String insert = "INSERT INTO wp_posts(post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged ,post_modified ,post_modified_gmt,post_content_filtered ,post_parent,guid,menu_order,post_type,post_mime_type,comment_count) " +
					"VALUES (3, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), ?, ?, '', 'publish', 'open', 'open', '', ?, '', '', CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), '', '0', 'http://localhost/taiiwin/?p=1', '0', 'post', '', '0')";
			boolean flag = false;
			getConnection(db);
			
			PreparedStatement pStmt = conn.prepareStatement("SELECT ID FROM wp_posts WHERE post_name = ?");
			pStmt.setString(1, seoUrl);
			rs = pStmt.executeQuery();
			if(rs.next()) {
				flag = true;
			}
			
			if(!flag) {
				PreparedStatement pStmt0 = conn.prepareStatement(insert);
				pStmt0.setString(1, content);
				pStmt0.setString(2, title);
				pStmt0.setString(3, seoUrl);
				pStmt0.executeUpdate();
				pStmt0.close();
				
				
				PreparedStatement pStmt1 = conn.prepareStatement("SELECT ID FROM wp_posts ORDER BY ID DESC limit 1");
				ResultSet rs1 = pStmt1.executeQuery();
				int idResult = 0;
				if(rs1.next()) {
					idResult = rs1.getInt("ID");
				}
				rs1.close();
				pStmt1.close();
				
				
				PreparedStatement pStmt2 = conn.prepareStatement("SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE term_id = ?");
				pStmt2.setInt(1, category);
				ResultSet rs2 = pStmt2.executeQuery();
				int idTaxonomy = 0;
				if(rs2.next()) {
					idTaxonomy = rs2.getInt("term_taxonomy_id");
				}
				rs2.close();
				pStmt2.close();
				
				PreparedStatement pStmt3  = conn.prepareStatement("INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES(?,?,0)");
				pStmt3.setInt(1, idResult);
				pStmt3.setInt(2, idTaxonomy);
				pStmt3.executeUpdate();
				pStmt3.close();
				
				PreparedStatement pStmt4  = conn.prepareStatement("UPDATE wp_posts SET guid = ? WHERE id = ?");
				pStmt4.setString(1, site+"/?p="+idResult);
				pStmt4.setInt(2, idResult);
				pStmt4.executeUpdate();
				
				pStmt4.close();
				System.out.println("Site - "+site);
				System.out.println("OKEY - "+title);
			
			}  else {
				System.out.println("NOT OKEY - "+title);
			}
			
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}
		
	}
	public void updateTermTaxonomy(int category,String db) throws Exception {
		try {
			
			getConnection(db);
			
			PreparedStatement pStmt1 = conn.prepareStatement("SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE term_id = ?");
			pStmt1.setInt(1, category);
			ResultSet rs1 = pStmt1.executeQuery();
			int idTaxonomy = 0;
			if(rs1.next()) {
				idTaxonomy = rs1.getInt("term_taxonomy_id");
			}
			rs1.close();
			pStmt1.close();
			
			PreparedStatement pStmt2 = conn.prepareStatement("SELECT count(*) as countPost FROM wp_term_relationships WHERE term_taxonomy_id = ?");
			pStmt2.setInt(1, idTaxonomy);
			ResultSet rs2 = pStmt2.executeQuery();
			int countPost = 0;
			if(rs2.next()) {
				countPost = rs2.getInt("countPost");
			}
			rs2.close();
			pStmt2.close();
			
			PreparedStatement pStmt = conn.prepareStatement("UPDATE wp_term_taxonomy SET count = ? WHERE term_taxonomy_id = ?");
			pStmt.setInt(1, countPost);
			pStmt.setInt(2, idTaxonomy);
			pStmt.executeUpdate();
			
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}
	}
	public static void main(String[] args) {
		try {
			VietBao24hDAO bao24hDAO = new VietBao24hDAO();
			NewsGame newsGame = new NewsGame();
			
			ArrayList<VietBaoEntity> list = bao24hDAO.selectNews(4,2000);
			
			//----------- http://taiiwin.mobi
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(9,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://taiiwin.mobi","admin_taiwin");
			}
			newsGame.updateTermTaxonomy(9,"admin_taiwin");
			
			//----------- http://taiavatar.mobi
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(7,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://taiavatar.mobi","admin_taiavatar");
			}
			newsGame.updateTermTaxonomy(7,"admin_taiavatar");
			
			//----------- http://taigamebigone.mobi
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(139,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://taigamebigone.mobi","admin_online24h");
			}
			newsGame.updateTermTaxonomy(139,"admin_online24h");
			
			//--------------- http://game-iwin.net
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(3,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://game-iwin.net","admin_iwinnet");
			}
			newsGame.updateTermTaxonomy(3,"admin_iwinnet");
			
			//--------------- http://game-iwin.net
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(57,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://taiiwin.biz","admin_iwinbiz");
			}
			newsGame.updateTermTaxonomy(57,"admin_iwinbiz");
	
			
			//--------------- http://taigamemobile.mobi
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(49,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://taigamemobile.mobi","admin_gamesmobi");
			}
			newsGame.updateTermTaxonomy(49,"admin_gamesmobi");
			
			//--------------- http://gameavatars.biz
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(7,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://gameavatars.biz","admin_gamesava");
			}
			newsGame.updateTermTaxonomy(7,"admin_gamesava");
			
			
			//--------------- http://downloadiwin.biz
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(3,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://downloadiwin.biz","admin_dliwinbiz");
			}
			newsGame.updateTermTaxonomy(3,"admin_dliwinbiz");
			
			
			//--------------- http://downloadiwin.mobi
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(3,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://downloadiwin.mobi","admin_dliwin");
			}
			newsGame.updateTermTaxonomy(3,"admin_dliwin");
			
			
			//--------------- http://downloadavatar.mobi
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(5,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://downloadavatar.mobi","admin_dlavatar");
			}
			newsGame.updateTermTaxonomy(5,"admin_dlavatar");
			
			
			//--------------- http://downloadavatar.org
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(7,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://downloadavatar.org","admin_avatarorg");
			}
			newsGame.updateTermTaxonomy(7,"admin_avatarorg");
			
			//--------------- http://downloadavatar.net
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(3,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://downloadavatar.net","admin_avatarnet");
			}
			newsGame.updateTermTaxonomy(3,"admin_avatarnet");
			
			//--------------- http://downloadavatar.info
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(3,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://downloadavatar.info","admin_avatarinfo");
			}
			newsGame.updateTermTaxonomy(3,"admin_avatarinfo");
			
			//--------------- http://downloadavatar.biz
			for(VietBaoEntity baoEntity : list) {
				newsGame.insertPost(7,baoEntity.getContent(),baoEntity.getTitle(),baoEntity.getSeoUrl(),"http://downloadavatar.biz","admin_avatarbiz");
			}
			newsGame.updateTermTaxonomy(7,"admin_avatarbiz");
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
